package org.jeecg.modules.work.dispatch.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.constant.BikeConstant;
import org.jeecg.modules.utils.RandomCodeUtils;
import org.jeecg.modules.work.dispatch.entity.BikeWorkDispatch;
import org.jeecg.modules.work.dispatch.service.IBikeWorkDispatchService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.work.repair.entity.BikeWorkRepair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 调度工单
 * @Author: 月月鸟
 * @Date:   2021-05-31
 * @Version: V1.0
 */
@Api(tags="调度工单")
@RestController
@RequestMapping("/dispatch/bikeWorkDispatch")
@Slf4j
public class BikeWorkDispatchController extends JeecgController<BikeWorkDispatch, IBikeWorkDispatchService> {
	@Autowired
	private IBikeWorkDispatchService bikeWorkDispatchService;

	/**
	 * 分页列表查询
	 *
	 * @param bikeWorkDispatch
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@ApiOperation(value="调度工单-分页列表查询", notes="调度工单-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(BikeWorkDispatch bikeWorkDispatch,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<BikeWorkDispatch> queryWrapper = QueryGenerator.initQueryWrapper(bikeWorkDispatch, req.getParameterMap());
		Page<BikeWorkDispatch> page = new Page<BikeWorkDispatch>(pageNo, pageSize);
		IPage<BikeWorkDispatch> pageList = bikeWorkDispatchService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 /**
	  * 调度工单拼接 分页列表查询
	  *
	  * @param bikeWorkDispatch
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 @ApiOperation(value="调度工单拼接-分页列表查询", notes="调度工单拼接-分页列表查询")
	 @GetMapping(value = "/pageList")
	 public Result<?> pageList(BikeWorkDispatch bikeWorkDispatch,
									@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									HttpServletRequest req) {

		 return bikeWorkDispatchService.pageList(pageNo,pageSize,bikeWorkDispatch);
	 }
	 /**
	  *  根据id 派单
	  *
	  * @param webData
	  * @return
	  */
	 @AutoLog(value = "调度工单-派单",operateType = 3)
	 @ApiOperation(value="调度工单-派单", notes="调度工单-派单")
	 @PutMapping(value = "/dispatch")
	 public Result<?> dispatch(@RequestBody BikeWorkDispatch webData) {

		 BikeWorkDispatch bikeWorkDispatch = new BikeWorkDispatch();
		 bikeWorkDispatch.setId(webData.getId());
		 String yn = bikeWorkDispatchService.judgeDispatch(bikeWorkDispatch);
		 if (yn.equals(BikeConstant.y_n_yes)){
			 bikeWorkDispatch.setIsDispatch(BikeConstant.y_n_yes);
			 bikeWorkDispatch.setDispatchTime(new Date());
			 bikeWorkDispatchService.updateById(bikeWorkDispatch);
			 return Result.OK("派单成功!");
		 }else {
		 	return Result.error("不可重复派单");
		 }

	 }
	 /**
	  * 调度工单拼接 查看
	  ** @param id
	  * @return
	  */
	 @ApiOperation(value="调度工单拼接-查看", notes="调度工单拼接-查看")
	 @GetMapping(value = "/pageListById")
	 public Result<?> pageList(@RequestParam String id) {

		 return bikeWorkDispatchService.pageListById(id);
	 }
	/**
	 *   添加
	 *
	 * @param bikeWorkDispatch
	 * @return
	 */
	@AutoLog(value = "调度工单-添加")
	@ApiOperation(value="调度工单-添加", notes="调度工单-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody BikeWorkDispatch bikeWorkDispatch) {
		String randomCode = RandomCodeUtils.getRandomCode(BikeConstant.work_dispatch_open);
		if (StringUtils.isNotEmpty(bikeWorkDispatch.getIsDispatch())){
			if(bikeWorkDispatch.getIsDispatch().equals(BikeConstant.y_n_yes)){
				bikeWorkDispatch.setDispatchTime(new Date());
			}
		}
		bikeWorkDispatch.setCode(randomCode);
		bikeWorkDispatch.setSourceId(randomCode);
		bikeWorkDispatchService.save(bikeWorkDispatch);
		return Result.OK("添加成功！");
	}
	 /**
	  *
	  *
	  * @param bikeWorkDispatch
	  * @return
	  */
	 @AutoLog(value = "调度工单-再次派单")
	 @ApiOperation(value="调度工单-再次派单", notes="调度工单-再次派单")
	 @PostMapping(value = "/addAgain")
	 public Result<?> addAgain(@RequestBody BikeWorkDispatch bikeWorkDispatch) {
		 //如果原工单为新增 再次派单来源为 原工单id（用于相关历史工单）
		 BikeWorkDispatch workDispatch = new BikeWorkDispatch();
		 if (bikeWorkDispatch.getWorkSource().equals("1")){
			 workDispatch.setSourceId(bikeWorkDispatch.getCode());
		}
		 //-----------需要存储
		 workDispatch.setTaskDescribe(bikeWorkDispatch.getTaskDescribe());
		 workDispatch.setContent(bikeWorkDispatch.getContent());
		 //-----------需要修改
		 workDispatch.setUserId(bikeWorkDispatch.getUserId());
		 //备注
		 workDispatch.setMemo(bikeWorkDispatch.getMemo());
		 //紧急程度
		 workDispatch.setUrgentType(bikeWorkDispatch.getUrgentType());
		 //截止完成时间
		 workDispatch.setFinishTime(bikeWorkDispatch.getFinishTime());
		// 是否照片
		 workDispatch.setIsPicture(bikeWorkDispatch.getIsPicture());
	 	//是否解决
		 workDispatch.setIsSolve(BikeConstant.y_n_no);
		 //已派单
		 workDispatch.setIsDispatch(BikeConstant.y_n_yes);
	 	//新转工单时间
		 workDispatch.setDispatchTime(new Date());
	 	//新工单code
		 workDispatch.setCode(RandomCodeUtils.getRandomCode(BikeConstant.work_dispatch_open));
	 	//来源为再次派单
		 workDispatch.setWorkSource("4");
	 	bikeWorkDispatchService.save(workDispatch);
	 	return Result.OK("添加成功！");
	 }

	/**
	 *  编辑
	 *
	 * @param bikeWorkDispatch
	 * @return
	 */
	@AutoLog(value = "调度工单-编辑",operateType = 3)
	@ApiOperation(value="调度工单-编辑", notes="调度工单-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody BikeWorkDispatch bikeWorkDispatch) {
		bikeWorkDispatchService.updateById(bikeWorkDispatch);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "调度工单-通过id删除")
	@ApiOperation(value="调度工单-通过id删除", notes="调度工单-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		return bikeWorkDispatchService.deleteById(id);

	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "调度工单-批量删除")
	@ApiOperation(value="调度工单-批量删除", notes="调度工单-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		String[] split = ids.split(",");
		//判断派单情况
		String yn = bikeWorkDispatchService.judgeDelet(split);
		if (yn.equals(BikeConstant.y_n_yes)){
			this.bikeWorkDispatchService.removeByIds(Arrays.asList(split));
			return Result.OK("批量删除成功!");
		}else {
			return Result.error("无法删除已派单工单");
		}
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="调度工单-通过id查询", notes="调度工单-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		BikeWorkDispatch bikeWorkDispatch = bikeWorkDispatchService.getById(id);
		if(bikeWorkDispatch==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(bikeWorkDispatch);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param bikeWorkDispatch
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BikeWorkDispatch bikeWorkDispatch) {
        //return super.exportXls(request, bikeWorkDispatch, BikeWorkDispatch.class, "调度工单");
        return bikeWorkDispatchService.exportXls(request,bikeWorkDispatch);
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, BikeWorkDispatch.class);
    }

}
